﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Globalization;

namespace devtm.CommonInteractive.Types
{

    public enum TokenType
    {
        Word,
        EndOfFile
    }



    [System.Diagnostics.DebuggerStepThrough()]
    [Serializable()]
    public class LexicalAnalyzer
    {
        // Fields
        private string expression;
        private string _lastWord;
        private Match match;
        private Match match2;
        private const string Pattern1 = @",?\[?[\w\d.`,= ]+\]?";
        private const string Pattern2 = @"`\d+";
        private Match previousMatch;

        // Methods
        public LexicalAnalyzer(string expression)
        {
            this.expression = expression;
        }

        public TokenType MoveNext()
        {
            this.previousMatch = this.match;
            if (this.match == null)
            {
                this.match = Regex.Match(this.expression, Pattern1, RegexOptions.Compiled);
                this.match2 = null;
            }
            else
                this.match = this.match.NextMatch();
            
            if (this.match.Success)
            {
                this.match2 = Regex.Match(Current, Pattern2, RegexOptions.Compiled);
                _lastWord = Current;
                return TokenType.Word;
            }
                
            return TokenType.EndOfFile;
        }

        // Properties
        public string Current { get { return this.match.Value; } }
        public string LastWord { get { return _lastWord; } }
        public Match CurrentMatch { get { return this.match; } }
        public Match PreviousMatch { get { return this.previousMatch; } }
        public string Previous { get { return this.previousMatch.Value; } }
        public int Generic { get { return this.match2 != null && this.match2.Success ? int.Parse(this.match2.Value.Replace("`", "")) : 0; } }

        public string Value { get { return this.match.Value.Replace(",[", "").Replace("[", "").Replace("]", ""); } }

    }


}
